﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Dan{
	public class Graphs : MonoBehaviour
	{
    	public Transform pointPrefab;
    	[Range(10,100)]
    	public int resolution = 10;
    	Transform[] pointsArray;
		// [SerializeField,Range(0,2)]
		[SerializeField]
		FunctionLibrary.FunctionName function = default;
		


		void Awake()
		{
			
			float step = 2f/ resolution;
			var scale = Vector3.one *step;
			
			pointsArray = new Transform[resolution*resolution];
			
			for (int i = 0 ; i < pointsArray.Length ; i++){
				
				Transform point = Instantiate(pointPrefab);
				point.localScale = scale;
				point.SetParent(transform,false);
				pointsArray[i] =point;

			}
		}

    
		void Update()
		{
			
			FunctionLibrary.Function f = FunctionLibrary.GetFunction(function);
			float t = Time.time;
			float step = 2f /resolution;
			float v =0.5f*step-1f;
			for (int i = 0 , x = 0 ,z = 0 ; i < pointsArray.Length ;i++ , x++){
				if(x == resolution) {
					x = 0 ;
					z +=1;
					v = (z + 0.5f) *step - 1f;
				}
				float u = (x+0.5f)*step-1f;
				pointsArray[i].localPosition = f(u,v,t);
				
				
			}
		}
		
	}
}
